package com.sms.domain.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import com.sms.core.request.SendContext;

/**
 * @desc 发送记录
 * @author wjw
 * @time 2017年11月16日下午3:31:48
 */
@Entity
@Table(name = "b_sms_history")
public class SmsHistory {
    
    /** 配置id */
    @Id
    @GeneratedValue(generator = "UIDGenerator")
    @GenericGenerator(name = "UIDGenerator", strategy = "com.sms.common.persistence.UIDGenerator")
    @Column(length = 40)
    private String smsHistoryId;
    
    /** 机号码(多个号码用”,”分隔)，最多1000个号码 */
    @Column(length = 20)
    private String mobile;
    
    /** FIXME */
    @Column(length = 40)
    private String fromUser;
    
    /** FIXME */
    @Column(length = 40)
    private String toUser;
    
    /** 短信内容 */
    @Column(length = 500)
    private String content;
    
    /** 发送时间 */
    @Column
    private Date sendTime;
    
    /** 应用ID */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "app_id")
    private Application application;
    
    /** 发送结果，1成功，0失败 */
    @Column
    private int sendResult;
    
    /**
     * 构造函数
     */
    public SmsHistory() {
    }
    
    public String getSmsHistoryId() {
        return smsHistoryId;
    }
    
    public void setSmsHistoryId(String smsHistoryId) {
        this.smsHistoryId = smsHistoryId;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public int getSendResult() {
        return sendResult;
    }
    public void setSendResult(int sendResult) {
        this.sendResult = sendResult;
    }
    public String getFromUser() {
        return fromUser;
    }
    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }
    public String getToUser() {
        return toUser;
    }
    public void setToUser(String toUser) {
        this.toUser = toUser;
    }
    public Date getSendTime() {
        return sendTime;
    }
    public void setSendTime(Date sendTime) {
        this.sendTime = sendTime;
    }
    public Application getApplication() {
        return application;
    }
    public void setApplication(Application application) {
        this.application = application;
    }
    
    /**
     * 从SMS对象获取到实体对象
     * @param context xx
     * @return xx
     */
    public static SmsHistory fromSendSmsContext(SendContext context) {
        if (context == null) {
            return null;
        }
        SmsHistory sh = new SmsHistory();
        sh.setContent(context.getSendContent());
        sh.setMobile(context.getMobile());
        sh.setSendTime(new Date());
        sh.setApplication(context.getApplication());
        sh.setFromUser(context.getFromUser());
        sh.setToUser(context.getToUser());
        return sh;
    }
}
